package com.qyhl.webtv.module_live.teletext.mixlive;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.basiclib.utils.glide.GlideRoundTransform;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.live.MixListBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.R2;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
 * Created by qyhl on 2018/1/30.
 * 混合直播列表
 */
@Route(path = ARouterPathConstant.MIX_LIVE)
public class MixLiveListActivity extends BaseActivity implements MixLiveListContract.MixLiveView {
    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.listview)
    ListView listview;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;
    @BindView(R2.id.back_btn)
    ImageView backBtn;

    private MixLiveListPresenter mPresenter;
    @Autowired(name = "type")
    String type;
    @Autowired(name = "title")
    String title;
    private String newsId;

    private List<MixListBean> mDataList;

    private CommonAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.live_activity_mix_list;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new MixLiveListPresenter(this);
        initView();
        mPresenter.getList(newsId, type);
    }

    /**
     * 初始化
     */
    private void initView() {
        loadMask.setStatus(LoadingLayout.Loading);

        mDataList = new ArrayList<>();

        newsId = "0";
        if (StringUtils.isEmpty(type)) {
            type = "";
        }
        mTitle.setText(title);

        refresh.setEnableLoadMore(true);
        refresh.setRefreshFooter(new ClassicsFooter(this));
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setDisableContentWhenRefresh(true);
        refresh.setDisableContentWhenLoading(true);

        listview.setAdapter(mAdapter = new CommonAdapter<MixListBean>(this, R.layout.live_item_mixlive_list, mDataList) {
            @Override
            protected void convert(ViewHolder viewHolder, MixListBean item, int position) {
                ImageView cover = viewHolder.getView(R.id.cover);
                Glide.with(MixLiveListActivity.this)
                        .load(item.getCoverImg())
                        .apply(new RequestOptions().error(R.drawable.cover_normal_default).placeholder(R.drawable.cover_normal_default).transform(new GlideRoundTransform(4)))
                        .into(cover);

                TextView title = viewHolder.getView(R.id.title);
                title.setText(item.getName());

                TextView scanNum = viewHolder.getView(R.id.scan_num);
                if (item.getType() == 1) {
                    scanNum.setText(StringUtils.transformNum(item.getHits() + "") + "人看过");
                } else {
                    scanNum.setText(StringUtils.transformNum(item.getHits() + "") + "人听过");
                }
            }
        });


    }

    /**
     * 监听
     */
    @Override
    protected void setListener() {
        //重载
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                newsId = "0";
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getList(newsId, type);
            }
        });
        //加载
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                mPresenter.getList(newsId, type);
            }
        });
        //刷新
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                newsId = "0";
                mPresenter.getList(newsId, type);
            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mDataList.get(position).getType() == 1) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", mDataList.get(position).getId() + "");
                    RouterManager.router(ARouterPathConstant.LIVE_VIDEO, bundle);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", mDataList.get(position).getId() + "");
                    RouterManager.router(ARouterPathConstant.LIVE_RADIO, bundle);
                }
            }
        });

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 设置数据
     *
     * @param beanList
     * @param isMore
     */
    @Override
    public void setList(List<MixListBean> beanList, boolean isMore) {
        loadMask.setReloadButtonText("点击重试~~");
        loadMask.setStatus(LoadingLayout.Success);
        newsId = beanList.get(beanList.size() - 1).getId() + "";
        if (isMore) {
            refresh.finishLoadMore();
            mDataList.addAll(beanList);
        } else {
            refresh.finishRefresh();
            mDataList.clear();
            mDataList.addAll(beanList);
        }

        mAdapter.notifyDataSetChanged();
    }

    /**
     * 网络出错
     *
     * @param msg
     */
    @Override
    public void setNetError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.No_Network);
        loadMask.setNoNetworkText(msg);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 无内容
     *
     * @param msg
     */
    @Override
    public void setEmpty(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Empty);
        loadMask.setEmptyText(msg);
        loadMask.setReloadButtonText("点击重试~~");
    }

    /**
     * 出错
     *
     * @param msg
     */
    @Override
    public void setError(String msg) {
        refresh.finishRefresh();
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setErrorText(msg);
        loadMask.setReloadButtonText("点击重试~~");
    }

    @Override
    public void showLog(String msg) {
        Log.i("info", msg);
        refresh.finishLoadMore();
    }

    @Override
    public void showMessage(String msg) {
        Toasty.normal(this, msg).show();
        refresh.finishLoadMore();
    }

    @Override
    public void onResume() {
        super.onResume();
        //umeng
        MobclickAgent.onPageStart("混合音视频列表");
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        //umeng
        MobclickAgent.onPageEnd("混合音视频列表");
        MobclickAgent.onPause(this);
    }
}
